package com.zqxq.lottery.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.bo.BaseBO;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.bo.sys.SysNewsCMSBO;
import com.zqxq.lottery.entity.o2o.sys.SysMsgO2O;
import com.zqxq.lottery.entity.o2o.sys.SysNewsO2O;
import com.zqxq.lottery.entity.po.sys.SysMsgPO;
import com.zqxq.lottery.entity.po.sys.SysNewsPO;
import com.zqxq.lottery.entity.vo.sys.SysMsgVO;
import com.zqxq.lottery.entity.vo.sys.SysNewsVO;
import com.zqxq.lottery.service.sys.ISysNewsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@RequestMapping("/sysNews")
public class SysNewsController extends SysBaseController{

    @Autowired
    private ISysNewsService sysNewsService;
    @Autowired
    private YmlConfig ymlConfig;

    @ApiOperation("获取分页列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysNewsVO>> queryPageList(@RequestBody SysNewsCMSBO baseBO){
        Result result = new Result();
        try{
            Page<SysNewsPO> page = new Page(baseBO.getPageNo(), baseBO.getPageSize());
            Wrapper wrapper = new EntityWrapper();
            if(baseBO.getNewsSource() != null){
                wrapper.eq("source", baseBO.getNewsSource());
            }
            if(StrUtils.isNotEmpty(baseBO.getClassify())){
                wrapper.eq("classify", baseBO.getClassify());
            }
            if(StrUtils.isNotEmpty(baseBO.getSearchKey())){
                wrapper.like("title", baseBO.getSearchKey());
            }
            wrapper.eq("status",1);
            wrapper.orderBy("sort", false);
            wrapper.orderBy("create_time", false);
            page = sysNewsService.selectPage(page, wrapper);
            Page<SysNewsVO> voPage = SysNewsO2O.toPageVO(page);
            result.setData(voPage);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Result save(@RequestBody SysNewsVO vo){
        Result result = new Result();
        try{
            SysNewsPO po = SysNewsO2O.toPO(vo);
            if(vo.getId() == null){
                Long id = ymlConfig.getPkId();
                po.setId(id);
            }
            sysNewsService.insertOrUpdate(po);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

    @ApiOperation("保存阅读量")
    @PostMapping("/saveReaderNum")
    public Result saveReaderNum(Long id){
        Result result = new Result();
        try{
            if(id == null){
                return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
            }
            sysNewsService.updateReaderNum(id, 1);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

    @ApiOperation("获取咨询详情")
    @PostMapping("/get")
    public Result<SysNewsVO> get(Long id){
        Result result = new Result();
        try{
            if(id == null){
                return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
            }
            SysNewsPO sysNewsPO = sysNewsService.selectById(id);
            SysNewsVO sysNewsVO = SysNewsO2O.toVO(sysNewsPO);
            result.setData(sysNewsVO);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

    @PostMapping("/close")
    @ApiOperation("关闭资讯")
    public Result closeNews(Long id){
        if (id == null){
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        try {
            SysNewsPO sysNewsPO = new SysNewsPO();
            sysNewsPO.setId(id);
            sysNewsPO.setStatus(0);
            sysNewsService.updateById(sysNewsPO);
        } catch (Exception e){
            log.error("SysNewsController close",e);
            return new Result(CodeEnum.FAIL_100001.getCode(),CodeEnum.FAIL_100001.getMsg());
        }
        return new Result();
    }

}
